﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using EasierRdp.Util;

namespace EasierRdp.Models
{
    class RdpAllUsers : INode
    {
        public const string
            AttributeExpanded = "Expanded",
            ElementName = "AllUsers";
        public string Name
        {
            get { return "All Users"; }
        }

        public IEnumerable<INode> Children
        {
            get
            {
                return RdpContext.Users.Values;
            }
        }

        public bool Expanded { get; set; }

        public void FromXml(XElement parentElement, string elementName)
        {
            var element = parentElement.Element(elementName);
            Expanded = element.GetBoolean(AttributeExpanded);
            if (element != null)
            {
                foreach (var item in element.Elements(RdpUser.ElementName).Select(e => new RdpUser(e)))
                {
                    RdpContext.Users.Add(item.Id, item);
                }
            }
        }

        public XElement ToXml()
        {
            return new XElement(ElementName, new XAttribute(AttributeExpanded, Expanded), RdpContext.Users.Values.ToXml());
        }
    }
}
